﻿using System;
using Caliburn.Micro;
using MediaOwl.WebSearch.TranslatorService;

namespace MediaOwl.WebSearch.Data.Translation
{
    public class Translation : IResult
    {
        private readonly string translateTerm;
        private readonly string sourceLanguage;
        private readonly string targetLanguage;
        private readonly Action<string, GetTranslationsResponse> onSuccess;
        private readonly Action<Exception> onFail;
        private readonly LanguageServiceClient svcClient;
        private string primaryTranslation;


        public Translation(string translateTerm, string sourceLanguage, string targetLanguage, Action<string, GetTranslationsResponse> onSuccess, Action<Exception> onFail = null)
        {
            this.translateTerm = translateTerm;
            this.sourceLanguage = sourceLanguage;
            this.targetLanguage = targetLanguage;
            this.onSuccess = onSuccess;
            this.onFail = onFail;

            svcClient = new LanguageServiceClient();
        }

        public void Execute(ActionExecutionContext context)
        {
            svcClient.TranslateCompleted += (s, e) =>
                                                {
                                                    if (e.Error == null)
                                                    {
                                                        primaryTranslation = e.Result;
                                                        GetAlternativeTranslations();
                                                    }
                                                    else
                                                    {
                                                        if (onFail != null)
                                                            Caliburn.Micro.Execute.OnUIThread(() => onFail(e.Error));
                                                        Completed(this, new ResultCompletionEventArgs { Error = e.Error });
                                                    }
                                                };

            svcClient.TranslateAsync(BingDataAccess.AppId, translateTerm, sourceLanguage, targetLanguage, "text/plain", "general");
        }

        private void GetAlternativeTranslations()
        {

            svcClient.GetTranslationsCompleted += (s, e) =>
                                                      {
                                                          if (e.Error == null)
                                                          {
                                                              if (onSuccess != null)
                                                                  Caliburn.Micro.Execute.OnUIThread(() => onSuccess(primaryTranslation, e.Result));
                                                          }
                                                          else
                                                          {
                                                              if (string.IsNullOrEmpty(primaryTranslation))
                                                              {
                                                                  if (onFail != null)
                                                                      Caliburn.Micro.Execute.OnUIThread(() => onFail(e.Error));
                                                              }
                                                              else
                                                              {
                                                                  if (onSuccess != null)
                                                                      Caliburn.Micro.Execute.OnUIThread(() => onSuccess(primaryTranslation, null));
                                                              }
                                                          }
                                                          Completed(this, new ResultCompletionEventArgs { Error = e.Error });
                                                      };

            svcClient.GetTranslationsAsync(BingDataAccess.AppId, translateTerm, sourceLanguage, targetLanguage, 20, new TranslateOptions() );
        }

        public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };
    }
}